﻿using System;
using System.Collections.Generic;

namespace Cube.Data
{
    class PattenManager
    {
        private Cube cube;
        public PattenManager(Cube cube)
        {
            this.cube = cube;
        }
        private List<string> record = new List<string>();
        public List<string> GetRecord()
        {
            List<string> result = new List<string>();
            result.AddRange(record);
            return result;
        }
        private void AddToRecord(string last)
        {
            record.Add(last);
        }
        public string GetOpposite(string patten)
        {
            if (patten.Contains("'"))
            {
                patten = patten.Replace("'", "");
            }
            else
            {
                patten += "'";
            }
            return patten;
        }
        public void Random()
        {
            Random r = new Random();
            switch (r.Next(0, 6))
            {
                case 0:
                    U();
                    break;
                case 1:
                    R();
                    break;
                case 2:
                    F();
                    break;
                case 3:
                    D();
                    break;
                case 4:
                    L();
                    break;
                case 5:
                    B();
                    break;
            }

        }
        public void U()
        {
            cube.Rotate(Faces.Up, 1);
            AddToRecord("U");
        }
        public void R()
        {
            cube.Rotate(Faces.Right, 1);
            AddToRecord("R");
        }
        public void F()
        {
            cube.Rotate(Faces.Front, 1);
            AddToRecord("F");
        }
        public void D()
        {
            cube.Rotate(Faces.Down, 1);
            AddToRecord("D");
        }
        public void L()
        {
            cube.Rotate(Faces.Left, 3);
            AddToRecord("L");
        }
        public void B()
        {
            cube.Rotate(Faces.Behind, 1);
            AddToRecord("B");
        }
        public void _U()
        {
            cube.Rotate(Faces.Up, 3);
            AddToRecord("U'");
        }
        public void _R()
        {
            cube.Rotate(Faces.Right, 3);
            AddToRecord("R'");
        }
        public void _F()
        {
            cube.Rotate(Faces.Front, 3);
            AddToRecord("F'");
        }
        public void _D()
        {
            cube.Rotate(Faces.Down, 3);
            AddToRecord("D'");
        }
        public void _L()
        {
            cube.Rotate(Faces.Left, 1);
            AddToRecord("L'");
        }
        public void _B()
        {
            cube.Rotate(Faces.Behind, 3);
            AddToRecord("B'");
        }
        public void x()
        {
            cube.Rotate(Dimention.X, 1);
            AddToRecord("x");
        }
        public void y()
        {
            cube.Rotate(Dimention.Z, 1);
            AddToRecord("y");
        }
        public void z()
        {
            cube.Rotate(Dimention.Y, 1);
            AddToRecord("z");
        }
        public void _x()
        {
            cube.Rotate(Dimention.X, 3);
            AddToRecord("x'");
        }
        public void _y()
        {
            cube.Rotate(Dimention.Z, 3);
            AddToRecord("y'");
        }
        public void _z()
        {
            cube.Rotate(Dimention.Y, 3);
            AddToRecord("z'");
        }
        public void MU()
        {
            cube.Rotate(Faces.MU, 1);
            AddToRecord("MU");
        }
        public void MR()
        {
            cube.Rotate(Faces.MR, 1);
            AddToRecord("MR");
        }
        public void MF()
        {
            cube.Rotate(Faces.MF, 1);
            AddToRecord("MF");
        }
        public void _MU()
        {
            cube.Rotate(Faces.MU, 3);
            AddToRecord("MU'");
        }
        public void _MR()
        {
            cube.Rotate(Faces.MR, 3);
            AddToRecord("MR'");
        }
        public void _MF()
        {
            cube.Rotate(Faces.MF, 3);
            AddToRecord("MF'");
        }

        public void DoFromString(string patten)
        {
            switch (patten)
            {
                case "U":
                    U();
                    break;
                case "D":
                    D();
                    break;
                case "F":
                    F();
                    break;
                case "L":
                    L();
                    break;
                case "R":
                    R();
                    break;
                case "B":
                    B();
                    break;
                case "U'":
                    _U();
                    break;
                case "D'":
                    _D();
                    break;
                case "F'":
                    _F();
                    break;
                case "L'":
                    _L();
                    break;
                case "R'":
                    _R();
                    break;
                case "B'":
                    _B();
                    break;
                case "x":
                    x();
                    break;
                case "y":
                    y();
                    break;
                case "z":
                    z();
                    break;
                case "x'":
                    _x();
                    break;
                case "y'":
                    _y();
                    break;
                case "z'":
                    _z();
                    break;
                case "MU":
                    MU();
                    break;
                case "MR":
                    MR();
                    break;
                case "MF":
                    MF();
                    break;
                case "MU'":
                    _MU();
                    break;
                case "MR'":
                    _MR();
                    break;
                case "MF'":
                    _MF();
                    break;
                default:
                    throw new Exception("非法操作");
            }
        }
    }
}
